package net.lab0.google.ex3;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import net.lab0.tools.Pair;

public class Ex3
{
    public static void main(String[] args)
    {
        System.out.println("start");
        Scanner scanner = new Scanner(System.in);
        int tests = scanner.nextInt();
        scanner.nextLine();
        
        for (int i = 0; i < tests; ++i)
        {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            
            int count = getRecycledNubersCount(a, b);
            
            System.out.println("Case #" + (i + 1) + ": " + count);
        }
    }
    
    private static int getRecycledNubersCount(int a, int b)
    {
        int count = 0;
        
        for (int i = a; i <= b; ++i) // for each number in the interval
        {
            for (int j = 0; j < Math.log10(i); ++j) // for each split point
            {
                int candidate = getCandidateWithStringOperation(i, j);
                if (a <= i && i < candidate && candidate <= b)
                {
                     System.out.println("Found :(" + i + "," + candidate + ")");
                    count++;
                }
            }
        }
        
        return count;
    }
    
    private static int getCandidateWithStringOperation(int i, int j)
    {
        String in = Integer.toString(i);
        String out = in.substring(j + 1) + in.substring(0, j + 1);
        if (out.startsWith("0"))
        {
            return Integer.MAX_VALUE;
        }
        return Integer.parseInt(out);
    }
    
    private static int getCandidate(int i, int j)
    {
        int split = (int) Math.pow(10, j + 1);
        int counterSplit = (int) Math.pow(10, (int) Math.log10(i) - j);
        int lowerPart = i / split;
        int tmpHigherPart = (i % split);
        if (tmpHigherPart >= split)
        {
            return Integer.MAX_VALUE;
        }
        
        int higherPart = tmpHigherPart * counterSplit;
        int result = lowerPart + higherPart;
        
        /*
         * test if invalid because of leading zeros or equality
         */
        if (result > i)
        {
            return result;
        }
        else
        {
            return Integer.MAX_VALUE; // return max int to be sure to exceed the value of B
        }
    }
}
